"use strict";

import Vue from 'vue';
import axios from "axios";

// Full config:  https://github.com/axios/axios#request-config
axios.defaults.baseURL = 'https://api.x7654.cn/api/' || process.env.baseURL || process.env.apiUrl;
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

let config = {
  // baseURL: process.env.baseURL || process.env.apiUrl || ""
  // timeout: 60 * 1000, // Timeout
  // withCredentials: true, // Check cross-site Access-Control
};

const _axios = axios.create(config);

_axios.interceptors.request.use(
  function (config) {
    // Do something before request is sent
    return config;
  },
  function (error) {
    // Do something with request error
    return Promise.reject(error);
  }
);

// Add a response interceptor
_axios.interceptors.response.use(
  function (response) {
    // Do something with response data
    return response;
  },
  function (error) {
    // Do something with response error
    return Promise.reject(error);
  }
);

Plugin.install = function (Vue) {
  Vue.axios = _axios;
  window.axios = _axios;
  Object.defineProperties(Vue.prototype, {
    axios: {
      get() {
        return _axios;
      }
    },
    $axios: {
      get() {
        return _axios;
      }
    },
  });
};


let tmp = axios.defaults.baseURL;
Vue.prototype.$api =
  tmp.substr(-1) == "/" || tmp.substr(-1) == "\\"
    ? tmp.substr(0, tmp.length - 1)
    : tmp;

Vue.prototype.$del = function (v, keys = ['changeTime', 'createTime', 'useTime']) {
  if (typeof (keys) == 'string') keys = [keys];
  let obj = JSON.parse(JSON.stringify(v));
  let ret = {};
  for (const key in obj) {
    if (function (k) {
      for (let i = 0; i < keys.length; i++) {
        if (k == keys[i]) {
          keys.splice(i, 1);
          return false;
        }
      }
      return true;
    }(key)) {
      ret[key] = obj[key];
    }
  }
  return ret;
};
Vue.prototype.$cp = function (v, keys = false) {
  if (!v) return undefined;
  let obj = JSON.parse(JSON.stringify(v));
  if (!keys) return obj;
  // console.log(obj);
  let ret = {};
  for (const k in keys) {
    const key = keys[k];
    if (key in obj) {
      ret[key] = obj[key];
    }
  }
  return ret;
}
Array.prototype.id2item = function (params) {
  for (let index = 0; index < this.length; index++) {
    if (this[index].ID == params) return this[index];
  }
}
Array.prototype.id2xb = function (params) {
  for (let index = 0; index < this.length; index++) {
    if (this[index].ID == params) return index;
  }
  return -1;
}
Array.prototype.in_array = function (params, init = false) {
  for (const key in this) {
    if (this[key] == params) return true;
  }
  if (init) this.push(params);
  return false;
}
Array.prototype.sin_array = function (params, init = false) {
  for (const key in this) {
    if (this[key] === params) return true;
  }
  if (init) this.push(params);
  return false;
}
Array.prototype.formatData = function () {
  for (let i = 0; i < this.length; i++) {
    if (this[i].cars) {
      this[i].cars = this[i].cars.split("|");
      if (!this[i].cars[this[i].cars.length - 1]) this[i].cars.pop();
      // console.log(this[i].cars);
    }
    if (this[i].notice) {
      this[i].notice = this[i].notice.split("|");
      if (!this[i].notice[this[i].notice.length - 1]) this[i].notice.pop();
    }
    if (this[i].promise) {
      this[i].promise = this[i].promise.split("|");
      if (!this[i].promise[this[i].promise.length - 1]) this[i].promise.pop();
    }
    if (this[i].imgs) {
      // this[i].imgs = this[i].imgs.split("|");
      // if (!this[i].imgs[this[i].imgs.length - 1]) this[i].imgs.pop();
      if (typeof (this[i].imgs) == 'string') this[i].imgs = JSON.parse(this[i].imgs);
      try {
        this[i].imgs = this[i].imgs.map(a => {
          let ret = a;
          if (ret.url.substr(0, 1) == '/') ret.url = Vue.prototype.$api + ret.url;
          return ret;
        });

      } catch (error) {
        this[i].imgs = [];
      }
    }
    if (typeof (this[i].itemInfo) == 'string') this[i].itemInfo = JSON.parse(this[i].itemInfo);
    if (typeof (this[i].money) == 'string') this[i].money = JSON.parse(this[i].money);
    if (this[i].itemInfo && typeof (this[i].itemInfo.imgs) == 'string') {
      this[i].itemInfo.imgs = JSON.parse(this[i].itemInfo.imgs).map(a => {
        let ret = a;
        if (ret.url.substr(0, 1) == '/') ret.url = Vue.prototype.$api + ret.url;
        return ret;
      });
    }
  }
}

Vue.prototype.$userType = ['游客', '会员', '商家', '管理员'];


Vue.use(Plugin)

export default Plugin;
